package com.itheima.test.code14;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author df
 * @date 2021/8/22
 */
public class Ticket02 implements Runnable{
    private int ticket = 100;
    //Lock锁对象
    private ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            try {
                //获取锁
                lock.lock();
                if(ticket <= 0){
                    //卖完了
                    break;
                }else {
                    ticket--;
                    System.out.println(Thread.currentThread().getName()+"在卖票,还剩下"+ticket+"票");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //释放锁
                lock.unlock();
            }
        }
        }

    }

